package com.tools.commons.config;

import com.tools.commons.aspect.LogAspect;
import com.tools.commons.config.properties.I18nProperties;
import com.tools.commons.config.properties.OauthProperties;
import com.tools.commons.config.properties.SwaggerProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Slf4j
@Configuration
@EnableConfigurationProperties({SwaggerProperties.class, I18nProperties.class, OauthProperties.class})
@AllArgsConstructor
public class AutoConfiguration implements WebMvcConfigurer {
    SwaggerProperties swaggerProperties;
    I18nProperties i18nProperties;

    @Bean
    public Cache cache() {
        return new ConcurrentMapCache("memory");
    }


    @Bean
    public LogAspect logAop() {
        return new LogAspect();
    }

    // @Override
    // public void addInterceptors(InterceptorRegistry registry) {
    //     registry.addInterceptor(loginInterceptor())
    //             .excludePathPatterns("/swagger/**")
    //             .excludePathPatterns(SystemConstants.Api.Base + "/wx/**")
    //             .excludePathPatterns("/favicon.ico")
    //             .excludePathPatterns("/error")
    //             .excludePathPatterns(SystemConstants.Api.Base + "/sendNotice")
    //             .addPathPatterns(SystemConstants.Api.Base + "/**");
    //
    //     // 解析参数切换语言，其中Param表示切换语言的参数名
    //     // url?lang=en_US
    //     LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
    //     localeInterceptor.setParamName(i18nProperties.getParam());
    //     registry.addInterceptor(localeInterceptor);
    // }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源
        if (swaggerProperties.isEnable()) {
            registry.addResourceHandler("/swagger/**")
                    .addResourceLocations("classpath:/swagger/");
        }
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setDefaultEncoding(i18nProperties.getEncoding());
        messageSource.setBasename(i18nProperties.getMessages());
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言简体中文
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars", "|{}[]"));
        return factory;
    }

    // @Bean
    // public LogFilter logFilter(ApplicationEventPublisher eventPublisher) {
    //     return new LogFilter(eventPublisher);
    // }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return factory -> {
            ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
            factory.addErrorPages(errorPage404);
        };
    }

}
